package com.bang.study.aggs;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.global.InternalGlobal;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @ClassName: AggsTvsSearch
 * @author: BANG
 * @Date: 2020/01/15 13:56
 * @Version: 1.0
 * @Modified By:
 * @Desc:
 *
 *
 * GET /tvs/sales/_search
 * {
 *   "size": 0,
 *   "query": {
 *     "term": {
 *       "brand": {
 *         "value": "长虹"
 *       }
 *     }
 *   },
 *   "aggs": {
 *     "single_brand_avg_price": {
 *       "avg": {
 *         "field": "price"
 *       }
 *     },
 *     "all": {
 *       "global": {},
 *       "aggs": {
 *         "all_brand_avg_price": {
 *           "avg": {
 *             "field": "price"
 *           }
 *         }
 *       }
 *     }
 *   }
 * }
 */
public class AggsTvsSearch3 {

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true)
                .build();

        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)
        );

        SearchResponse searchResponse = client.prepareSearch("tvs").setTypes("sales")
                .setQuery(QueryBuilders.termQuery("brand","长虹"))
                .addAggregation(
                        AggregationBuilders.avg("single_brand_avg_price").field("price")
                        )
                .addAggregation(AggregationBuilders.global("all")
                                .subAggregation(AggregationBuilders.avg("all_brand_avg_price").field("price"))
                                )
                .setSize(0)
                .get();


        Aggregations aggregations = searchResponse.getAggregations();
        InternalAvg single_brand_avg_price = aggregations.get("single_brand_avg_price");
        String valueAsString = single_brand_avg_price.getValueAsString();
        System.err.println("single_brand_avg_price: "+valueAsString);
        InternalGlobal all = aggregations.get("all");
        List<Aggregation> aggregationList = all.getAggregations().asList();
        for (Aggregation aggregation : aggregationList) {
            InternalAvg internalAvg = (InternalAvg) aggregation;
            System.err.println(internalAvg.getName()+" : "+internalAvg.getValueAsString());
        }


        client.close();

    }
}
